/*
 * @Descripttion: 
 * @version: 
 * @Author: yuanliutech
 * @Date: 2022-11-01 14:23:34
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2022-11-26 09:59:49
 */
const app = getApp()
import Dialog from '@vant/weapp/dialog/dialog';
import {
    showMsg
} from '../../../../utils/config.js'
import {
    validateEmoji,
    validateSpecial
} from '../../../../utils/validate.js'
import {
    reqSpaceEdit
} from '../../../../request/api'
Page({
    data: {
        navHeight: app.globalData.navHeight + 'px', //头部距离
        bottomHeight: app.globalData.bottomHeight + 'px', //iphone底部距离
        parkId: '', //园区id
        form: {
            id: '',
            name: '',
            address: '',
            lng: '22.22',
            lat: '134.45',
            isDefence: false,
            buildingType: 2, //空间类型 
        },
        disabledBtn: false,
    },
    onLoad(e) {
        this.getItemInfo(e)
    },
    // 获取信息
    getItemInfo(e) {
        let item = JSON.parse(e.item)
        console.log(item);
        let a=item.name.indexOf("层")
        let l =item.name.substring(0,a)
        this.setData({
            parkId: item.parkId,
            'form.id': item.id,
            'form.name': l,
            'form.address': item.address,
            'form.lng': item.lng,
            'form.lat': item.lat,
            'form.isDefence': item.isDefence === 1 ? true : false,
        })
    },
    // 点击布撤防
    changeDefence(e) {
        let tip = e.detail ? '此操作将开启该楼层所有设备布防设置，是否继续？' : '此操作将关闭该楼层所有设备布防设置，是否继续？'
        Dialog.confirm({
                title: '温馨提示',
                message: tip,
            })
            .then(async () => {
                this.setData({
                    "form.isDefence": e.detail
                })
            })
            .catch(() => {});
    },

    // 选择单个添加/批量添加
    radioChange(e) {
        this.setData({
            radio: e.detail
        })
    },
    // 单个添加获取名称
    nameChange(e) {
         // 正则表达式
         let value = e.detail.value.replace(/[^\-\d]/g, ""); // 只能输入-和数字
         value = value.replace(/\-{2,}/g, "-"); // -只能保留一个
         value = value.replace(/(\d)\-/g, "$1"); // 数字后面不能接-，不能出现类似-11-2,12-，11-23
         value = value.replace(/-(0+)/g, "0"); // 不能出现-0,-001,-0001类似
         value = value.replace(/^0+(\d)/, "$1"); // 第一位0开头，0后面为数字，则过滤掉，取后面的数字
         if(value>50){
            value=50
         }
         if(value<-3){
            value=-3
         }
         if(value=='0'){
            value=1
         }
        // this.data.form.name = validateEmoji(e.detail.value.replace(/\s+/g, ''))
        this.setData({
            "form.name": value
        })
    },
    // 单个添加清除
    clearname() {
        this.setData({
            'form.name': ''
        })
    },
    /**
     * @name: 确认新增楼层
     * @msg: *orgId:组织id  *parkId：园区id *name：名称    *isDefence：布撤防  *buildingType：空间类型  childrenName批量新增下级空间名称
     * @return {*}
     */

    confirm() {
        if (this.data.disabledBtn) return
        let {
            form
        } = this.data
        if (!(form.name)) return showMsg("请输入名称")
        if (!validateSpecial(form.name)) return
        let params = {
            parkId: this.data.parkId,
            id: form.id,
            name: form.name+'层',
            address: form.address,
            lng: form.lng,
            lat: form.lat,
            isDefence: form.isDefence ? 1 : 0,
            buildingType: form.buildingType,
        }
        this.reqSpaceAddFun(params)
    },
    // 单个添加
    async reqSpaceAddFun(params) {
        const res = await reqSpaceEdit(params)
        console.log(res);
        if (res.code != 20000) return showMsg(res.message, 'error');
        showMsg("编辑成功", "success")
        this.setData({
            disabledBtn: true
        })
        setTimeout(() => {
            wx.navigateBack({
                delta: 1,
            })
        }, 2000)
    },

})